package com.rppowell.pixie.swing.spriteset;

import com.rppowell.pixie.models.Sprite;
import com.rppowell.pixie.models.SpriteSet;
import com.rppowell.pixie.swing.PixieUIState;
import com.rppowell.pixie.swing.mapset.MapSetTabbedPane;
import com.rppowell.pixie.swing.sprite.SpriteWorkArea;
import java.awt.CardLayout;
import java.awt.Component;
import javax.swing.JPanel;

public class SpriteSetWorkArea extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SpriteSet spriteSet;
	private final String SPRITE_MODE = "SPRITE_MODE";
	private final String MAPS_MODE = "MAP_MODE";
	
	public SpriteSetWorkArea(SpriteSet s) {
		spriteSet = s;
		setLayout(new CardLayout());
		
		add(new SpriteWorkArea(spriteSet), SPRITE_MODE);
		add(new MapSetTabbedPane(spriteSet), MAPS_MODE);
	}
	
	public void changeMode(String s) {
	    CardLayout cl = (CardLayout)(getLayout());
	    cl.show(this, s);
	}

	public void setPixieUIState(PixieUIState state) {            
		if (state.isSpriteMode()) {
			changeMode(this.SPRITE_MODE);
		} else if (state.isMapsMode()) {
			changeMode(this.MAPS_MODE);			
		} else {
                        throw new UnsupportedOperationException("Not supported yet.");
		}		
	}
        
        public SpriteSet getSpriteSet() {
            return spriteSet;
        }
        
        public void setSelectedSprite(Sprite sprite) {
            Component[] components = getComponents();
            for (int i = 0; i < components.length; i++) {
                Component c = components[i];
                if (c instanceof SpriteWorkArea) {
                    SpriteWorkArea spriteworkarea = (SpriteWorkArea) c;
                    spriteworkarea.setSelectedSprite(sprite);
                }
            }
        }
}
